package com.richper.springcloudh2020.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.richper.springcloudh2020.service.HystrixOrderService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

@RestController
@RequestMapping("/consumer")
@DefaultProperties(defaultFallback = "payment_timeOutHandler")
public class HystrixOrderController {
    @Resource
    private HystrixOrderService hystrixOrderService;

    @GetMapping("/payment_ok")
    public String payment_ok(){
        return hystrixOrderService.payment_ok();
    }

    @GetMapping("/payment_timeOut")
//    @HystrixCommand(fallbackMethod = "payment_timeOutHandler",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
//    })
    @HystrixCommand
    public String payment_timeOut(){
        return hystrixOrderService.payment_timeOut();
    }

    public String payment_timeOutHandler(){
        return "线程池："+Thread.currentThread().getName()+"  80系统忙，请稍后再试。,  ┭┮﹏┭┮";
    }

    @GetMapping("/div/{id}")
    @HystrixCommand(fallbackMethod = "divFuse",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),//开启断路器，默认为false
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),// 时间范围内请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),//时间范围
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")//时间范围内达到60%的错误率
    })
    public String div(@PathVariable("id") int id){
       return hystrixOrderService.div(id);
    }

    public String divFuse(int id){
        return Thread.currentThread().getName()+" ：80服务异常请稍后再试...id不能为0";
    }
}
